#include "gamepanel.h"
#include "playhand.h"
#include "ui_gamepanel.h"
#include"gamecontrol.h"
#include "endingpanel.h"
#include <QMouseEvent>
#include<QPixmap>
#include<QRandomGenerator>
#include<QDebug>
#include <QPropertyAnimation>
GamePanel::GamePanel(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::GamePanel)
{
    ui->setupUi(this);


    int num = QRandomGenerator::global()->bounded(10);
    QString path = QString(":/images/images/background-%1.png").arg(num+1);
    m_pixmap.load(path);

    setWindowTitle("欢乐斗地主");
    setFixedSize(1000,650);

    gameControlInit(); //实例化游戏控制类对象

    updatePlayerScore(); //更新游戏分数

    initCardMap(); //切割图片

    initButtonsGroup();//初始化按钮组

    initPlayerContest();

    initGameScene();


    m_animaltion = new Animaltionwindow(this);
    m_timer = new QTimer(this);
    connect(m_timer,&QTimer::timeout,this,&GamePanel::onDispatchCard);

    //showEndingScorePanel();
    // QTimer::singleShot(2000,this,[=](){
    //     showAnimaltion(JokerBomb);
    // });
}

GamePanel::~GamePanel()
{
    delete ui;
}
void GamePanel::showEndingScorePanel()
{
    qDebug()<<"show endingpanel";
    bool isLord = m_gamecontrol->getUserPlayer()->getRole() == (Player::Lord) ?true:false;
    bool isWin = m_gamecontrol->getUserPlayer()->isWin();
    EndingPanel *panel = new EndingPanel(isLord,isWin,this);
    panel->move((width()-panel->width())/2,-panel->height());
    panel->setPlayerScore(m_gamecontrol->getLeftRobot()->getScore(),
                          m_gamecontrol->getRightRobot()->getScore(),
                          m_gamecontrol->getUserPlayer()->getScore());
    panel->show();
    QPropertyAnimation *animation = new QPropertyAnimation(panel,"geometry",this);
    animation->setDuration(1500);
    animation->setStartValue(QRect(panel->x(),panel->y(),panel->width(),panel->height()));
    animation->setEndValue(QRect((width()-panel->width())/2,(height()-panel->height())/2,
                                 panel->width(),panel->height()));
    animation->setEasingCurve(QEasingCurve(QEasingCurve::OutBounce));
    animation->start();

    connect(panel,&EndingPanel::ContinueGame,this,[=](){
        panel->close();
        panel->deleteLater();
        animation->deleteLater();
        ui->btnGroup->selectPage(ButtonGroup::Empty);
        gameStatusPrecess(GameControl::dispatchCard);
    });


}
void GamePanel::gameControlInit()
{
    m_gamecontrol = new GameControl(this);
    m_gamecontrol->playerInit();
    Robot *leftRobot = m_gamecontrol->getLeftRobot();
    Robot *rightRobot = m_gamecontrol->getRightRobot();
    UserPlayer *user = m_gamecontrol->getUserPlayer();
    m_playerList<<leftRobot<<rightRobot<<user;

    connect(m_gamecontrol,&GameControl::playerStatusChanged,this,&GamePanel::onPlayerStatusChanged);
    connect(m_gamecontrol,&GameControl::notifyGrabLordBet,this,&GamePanel::onGrabLordBet);
    connect(m_gamecontrol,&GameControl::gameStatusChanged,this,&GamePanel::gameStatusPrecess);

    connect(m_gamecontrol,&GameControl::notifyPlayHand,this,&GamePanel::DisposePlayHand);

    connect(leftRobot,&Player::notifyPickCard,this,&GamePanel::disposeCard);
    connect(rightRobot,&Player::notifyPickCard,this,&GamePanel::disposeCard);
    connect(user,&Player::notifyPickCard,this,&GamePanel::disposeCard);
}

void GamePanel::updatePlayerScore()
{
    ui->ScorePanel1->setScore(
	m_playerList[0]->getScore(),
	m_playerList[1]->getScore(),
	m_playerList[2]->getScore());
}

void GamePanel::initCardMap()
{
    QPixmap pixmap(":/images/images/card.png");
    m_cardSize.setWidth(pixmap.width()/13);
    m_cardSize.setHeight(pixmap.height()/5);

    m_cardbackimage = pixmap.copy(m_cardSize.width()*2,m_cardSize.height()*4
                                  ,m_cardSize.width(),m_cardSize.height());
    for(int i =0,suit = Card::Suit_Begin+1;i<Card::Suit_End;++i,++suit)
    {
        for(int j = 0 ,point = Card::Card_Begin+1;j<Card::Card_SJ;++j,++point)
        {
            Card card((Card::CardPoint)point,(Card::CardSuit)suit);
            cropimage(pixmap,j*m_cardSize.width(),i*m_cardSize.height(),card);
        }
    }

    Card c;
    c.setPoint(Card::Card_SJ);
    c.setSuit(Card::Suit_Begin);
    cropimage(pixmap,0,4*m_cardSize.height(),c);

    c.setPoint(Card::Card_BJ);
    cropimage(pixmap,m_cardSize.width(),4*m_cardSize.height(),c);

}

void GamePanel::cropimage(QPixmap &pix, int x, int y,Card& c)
{

    QPixmap sub = pix.copy(x,y,m_cardSize.width(),m_cardSize.height());
    CardPanel *panel = new CardPanel(this);
    panel->setImage(sub,m_cardbackimage);
    panel->setCard(c);
    panel->hide();
    m_cardmap.insert(c,panel);
    connect(panel,&CardPanel::CardSelected,this,&GamePanel::onCardSelected);
}

void GamePanel::initButtonsGroup()
{
    ui->btnGroup->initButton();
    ui->btnGroup->selectPage(ButtonGroup::start);

    connect(ui->btnGroup,&ButtonGroup::startGame,this,[=](){
        ui->btnGroup->selectPage(ButtonGroup::Empty);
        m_gamecontrol->clearPlayerScore();
        updatePlayerScore();
        gameStatusPrecess(GameControl::dispatchCard);
        onDispatchCard();
    });
    connect(ui->btnGroup,&ButtonGroup::playHand,this,&GamePanel::onUserPlayHand);
    connect(ui->btnGroup,&ButtonGroup::pass,this,&GamePanel::onUserPass);
    connect(ui->btnGroup,&ButtonGroup::betPoint,this,[=](int bet){
        m_gamecontrol->getUserPlayer()->grabLordBet(bet);
        ui->btnGroup->selectPage(ButtonGroup::Empty);
    });
}

void GamePanel::initPlayerContest()
{
    const QRect cardsRect[] =
    {
        QRect(90,130,100,height()-200),
        QRect(rect().right()-190,130,100,height()-200),
        QRect(250,rect().bottom()-120,width()-500,100)
    };
    const QRect playHandRect[] =
        {
            QRect(260,150,100,100),
            QRect(rect().right()-360,150,100,100),
            QRect(150,rect().bottom()-290,width()-300,105)
        };
    const QPoint roleImgPos[]=
    {
        QPoint(cardsRect[0].left()-80, cardsRect[0].height() / 2 + 20),     // 左侧机器人
        QPoint(cardsRect[1].right()+10, cardsRect[1].height() / 2 + 20),    // 右侧机器人
        QPoint(cardsRect[2].right()-10, cardsRect[2].top() - 10)
    };

    int index = m_playerList.indexOf(m_gamecontrol->getUserPlayer());
    for(int i = 0 ; i<m_playerList.size();++i)
    {
        PlayerContext context;
        context.align = (i==index ? Horizontal : Vertical);
        context.isFrontSize = (i == index ?true :false);
        context.cardRect = cardsRect[i];
        context.playHandRect = playHandRect[i];
        context.info = new QLabel(this);
        context.info->resize(160,98);
        context.info->hide();
        QRect rect = playHandRect[i];
        QPoint point(rect.left()+(rect.width()-context.info->width())/2,
                     rect.top()+(rect.height()-context.info->height())/2);
        context.info->move(point);

        context.roleimg = new QLabel(this);
        context.roleimg->resize(84,120);
        context.roleimg->hide();
        context.roleimg->move(roleImgPos[i]);

        m_contextMap.insert(m_playerList.at(i),context);
    }
}

void GamePanel::initGameScene()
{
    m_baseCard = new CardPanel(this);
    m_baseCard->setImage(m_cardbackimage,m_cardbackimage);

    m_moveCard = new CardPanel(this);
    m_moveCard->setImage(m_cardbackimage,m_cardbackimage);

    for(int i = 0 ;i<3;++i)
    {
        CardPanel *panel = new CardPanel(this);
        panel->setImage(m_cardbackimage,m_cardbackimage);
        panel->hide();
        m_last3Card.push_back(panel);
    }

    m_baseCardPos = QPoint((width()-m_cardSize.width())/2,height()/2-100);
    m_baseCard->move(m_baseCardPos);
    m_moveCard->move(m_baseCardPos);

    int base = (width()-3*m_cardSize.width()-2*10)/2;
    for(int i =0 ; i<3 ; ++i)
    {
        m_last3Card[i]->move(base+(m_cardSize.width()+10)*i,20);
    }


}

void GamePanel::gameStatusPrecess(GameControl::GameStatus statu)
{
    m_status = statu;
    switch (statu) 
	{
    case GameControl::dispatchCard:
        startDispatchCard();
        break;
    case GameControl::CallingLord:
    {
        update();
        QVector<Card> last3card = m_gamecontrol->getSurplusCards().toCardList();
        for(int i = 0 ;i<last3card.size();++i)
        {
            QPixmap front = m_cardmap[last3card.at(i)]->getImage();
            m_last3Card[i]->setImage(front,m_cardbackimage);
            m_last3Card[i]->hide();
        }

        m_gamecontrol->startLandLord();
        break;
    }
    case GameControl::PlaingHand:
        m_baseCard->hide();
        m_moveCard->hide();
        //m_animaltion->hide();
        update();
        for(int i =0;i<m_last3Card.size();++i)
        {
            m_last3Card.at(i)->show();
        }
        for(int i = 0;i<m_playerList.size();++i)
        {
            PlayerContext &context = m_contextMap[m_playerList.at(i)];
            context.info->hide();

            Player *player = m_playerList.at(i);
            QPixmap pixmap = loadRoleImage(player->getSex(),player->getDirection(),player->getRole());
            context.roleimg->setPixmap(pixmap);
            context.roleimg->show();
        }
        break;
    default:
        break;
    }
    
}

void GamePanel::startDispatchCard()
{
    for(auto it = m_cardmap.begin();it!=m_cardmap.end();it++)
    {
        it.value()->setSelected(false);
        it.value()->setFrontsize(true);
        it.value()->hide();
    }
    for(int i = 0 ;i<m_last3Card.size();i++)
    {
        m_last3Card.at(i)->hide();
    }
    int index = m_playerList.indexOf(m_gamecontrol->getUserPlayer());
    for(int i = 0 ;i<m_playerList.size();i++)
    {
        m_contextMap[m_playerList.at(i)].lastCards.clear();
        m_contextMap[m_playerList.at(i)].info->hide();
        m_contextMap[m_playerList.at(i)].roleimg->hide();
        m_contextMap[m_playerList.at(i)].isFrontSize = (i==index ?true:false);

    }
    m_gamecontrol->resetCardsData();
    m_baseCard->show();
    ui->btnGroup->selectPage(ButtonGroup::Empty);
    m_timer->start(10);
    update();
}

void GamePanel::onDispatchCard()//发牌及其动画
{
    static int curMovePos = 0 ;
    Player *curplayer = m_gamecontrol->getCurPlayer();
    if(curMovePos>=100)
    {
        Card card = m_gamecontrol->takeOneCard();
        curplayer->storeDispatchCard(card);
        Cards cs(card);
        //disposeCard(curplayer,cs);
        m_gamecontrol->setCurPlayer(curplayer->getnextPlayer());
        curMovePos = 0 ;

        cardMoveStep(curplayer,curMovePos);

        if(m_gamecontrol->getSurplusCards().cardCount() == 3)
        {
            m_timer->stop();
            gameStatusPrecess(GameControl::CallingLord);
            return;
        }

    }
    cardMoveStep(curplayer,curMovePos);
    curMovePos +=15;

}

void GamePanel::cardMoveStep(Player *player, int curPos)
{
    QRect cardRect = m_contextMap[player].cardRect;
    int unit[] = {
        (m_baseCardPos.x() - cardRect.right())/100,
        (cardRect.left() - m_baseCardPos.x())/100,
        (cardRect.top() - m_baseCardPos.y())/100
    };
    QPoint pos[] = {
        QPoint(m_baseCardPos.x() - curPos*unit[0],m_baseCardPos.y()),
        QPoint(m_baseCardPos.x() + curPos*unit[1],m_baseCardPos.y()),
        QPoint(m_baseCardPos.x() , m_baseCardPos.y()+curPos*unit[2])
    };
    int index = m_playerList.indexOf(player);
    m_moveCard->move(pos[index]);
    update();
    if(curPos == 0 )
    {
        m_moveCard->show();
    }
    if(curPos == 100)
    {
        m_moveCard->hide();
    }
}

void GamePanel::disposeCard(Player *player,const Cards &cards)
{
    Cards& myCard = const_cast<Cards&>(cards);
    QVector<Card> list = myCard.toCardList();
    for(int i = 0; i<list.size();++i)
    {
        CardPanel *panel = m_cardmap[list.at(i)];
        panel->setOwner(player);
    }
    updataPlayerCards(player);
}

void GamePanel::updataPlayerCards(Player *player)
{
    Cards cards = player->getCards();
    QVector<Card> list = cards.toCardList();

    int cardspace = 20;
    QRect cardRect = m_contextMap[player].cardRect;
    for(int i = 0 ;i<list.size();++i)
    {
        CardPanel *panel = m_cardmap[list.at(i)];
        panel->show();
        panel->raise();
        panel->setFrontsize(m_contextMap[player].isFrontSize);
        if(m_contextMap[player].align == Horizontal)
        {
            int leftX = cardRect.left() + (cardRect.width()-(list.size()-1)*cardspace-panel->width())/2;
            int topY = cardRect.top() + (cardRect.height()-m_cardSize.height())/2;
            if(panel->getSelected())
            {
                topY -=10;
            }
            panel->move(leftX+cardspace*i,topY);
            m_cardRect = QRect(leftX,topY,cardspace*i+m_cardSize.width(),m_cardSize.height());
            int curWidth = 0;
            if(list.size()-1==i)
            {
                curWidth = m_cardSize.width();
            }
            else
            {
                curWidth = cardspace;
            }
            QRect cardRect(leftX+cardspace*i,topY,curWidth,m_cardSize.height());
            m_usercard.insert(panel,cardRect);
        }
        else
        {
            int leftX = cardRect.left()+(cardRect.width()-m_cardSize.width())/2;
            int topY = cardRect.top() + (cardRect.height()-(list.size()-1)*cardspace-panel->height())/2;
            panel->move(leftX,topY+cardspace*i);
        }
    }
    QRect playCardsRect = m_contextMap[player].playHandRect;
    Cards lastCards = m_contextMap[player].lastCards;
    if(!lastCards.isEmpty())
    {
        int playSpacing = 24;
        QVector<Card> lastCardList = lastCards.toCardList();
        QVector<Card>::ConstIterator itplayed = lastCardList.constBegin();
        for(int i =0 ; itplayed!=lastCardList.constEnd();++itplayed,i++)
        {
            CardPanel *panel = m_cardmap[*itplayed];
            panel->setFrontsize(true);
            panel->raise();
            if(m_contextMap[player].align == Horizontal)
            {
                int leftBase = playCardsRect.left() +
                               (playCardsRect.width() - (lastCardList.size()-1)*playSpacing - panel->width())/2;
                int top = playCardsRect.top() +(playCardsRect.height() - panel->height())/2;
                panel->move(leftBase+i*playSpacing,top);
            }
            else
            {
                int left = playCardsRect.left() + (playCardsRect.width()-panel->width())/2;
                int top = playCardsRect.top();
                panel->move(left,top+i*playSpacing);
            }
            panel->show();
        }

    }

}

void GamePanel::onPlayerStatusChanged(Player *player, GameControl::PlayerStatus status)
{
    switch (status) {
    case GameControl::ThkingForCallLord:
    {
        if(m_gamecontrol->getUserPlayer()==player)
        {
            ui->btnGroup->selectPage(ButtonGroup::CallLord,m_gamecontrol->getmaxLordbet());
        }
        break;
    }
    case GameControl::ThkingForPlayHand:
    {
        hidePlayerDropCards(player);

        if(player == m_gamecontrol->getUserPlayer())
        {
            Player *pendPlayer = m_gamecontrol->getPendPlayer();
            if(pendPlayer == m_gamecontrol->getUserPlayer() || pendPlayer == nullptr)
            {
                ui->btnGroup->selectPage(ButtonGroup::PlayCard);
            }
            else
            {
                ui->btnGroup->selectPage(ButtonGroup::PassOrPlay);
            }
        }
        else
        {
            ui->btnGroup->selectPage(ButtonGroup::Empty);
        }
        break;
    }
    case GameControl::Winning:
    {
        m_contextMap[m_gamecontrol->getLeftRobot()].isFrontSize = true;
        m_contextMap[m_gamecontrol->getRightRobot()].isFrontSize = true;
        updataPlayerCards(m_gamecontrol->getLeftRobot());
        updataPlayerCards(m_gamecontrol->getRightRobot());
        updatePlayerScore();
        showEndingScorePanel();
        m_gamecontrol->setCurPlayer(player);

        break;
    }
    default:
        break;
    }
}

void GamePanel::onGrabLordBet(Player *player, int bet, bool flag)
{
    PlayerContext context = m_contextMap[player];
    if(bet == 0)
    {
        context.info->setPixmap(QPixmap(":/images/images/buqinag.png"));
    }
    else
    {
        if(flag)
        {
            context.info->setPixmap(QPixmap(":/images/images/jiaodizhu.png"));
        }
        else
        {
            context.info->setPixmap(QPixmap(":/images/images/qiangdizhu.png"));
        }
    }
    context.info->show();
    showAnimaltion(AnimaltionType::Bet,bet);
}

void GamePanel::showAnimaltion(AnimaltionType type, int bet)
{
    switch (type) {
    case AnimaltionType::ShunZi:
    case AnimaltionType::LianDui:
        m_animaltion->setFixedSize(250,150);
        m_animaltion->move((width()-m_animaltion->width())/2,200);
        m_animaltion->showSequence((Animaltionwindow::Type)type);
        break;
    case AnimaltionType::Plane:
        m_animaltion->setFixedSize(800,75);
        m_animaltion->move((width()-m_animaltion->width())/2,200);
        m_animaltion->showPlane();
        break;
    case AnimaltionType::JokerBomb:
        m_animaltion->setFixedSize(250,150);
        m_animaltion->move((width()-m_animaltion->width())/2,(height()-m_animaltion->height())/2-70);
        m_animaltion->showJokerBomb();
        break;
    case AnimaltionType::Bomb:
        m_animaltion->setFixedSize(180,200);
        m_animaltion->move((width()-m_animaltion->width())/2,(height()-m_animaltion->height())/2-70);
        m_animaltion->showBomb();
        break;
    case AnimaltionType::Bet:
        m_animaltion->setFixedSize(160,98);
        m_animaltion->move((width()-m_animaltion->width())/2,(height()-m_animaltion->height())/2-140);
        m_animaltion->showBetScore(bet);
        break;
    }
    m_animaltion->show();
}

void GamePanel::DisposePlayHand(Player *player, Cards &cards)
{

    QMap<Player*, PlayerContext>::iterator it = m_contextMap.find(player);
    it->lastCards = cards;

    PlayHand hand(cards);
    PlayHand::HandType type = hand.getHandType();
    if(type == PlayHand::Hand_Plane || type ==PlayHand::Hand_Plane_Two_Pair || type ==PlayHand::Hand_Plane_Two_Single)
    {
        showAnimaltion(Plane);
    }
    else if(type == PlayHand::Hand_Seq_Single)
    {
        showAnimaltion(ShunZi);
    }
    else if(type == PlayHand::Hand_Seq_Pair)
    {
        showAnimaltion(LianDui);
    }
    else if(type == PlayHand::Hand_Bomb)
    {
        showAnimaltion(Bomb);
    }
    else if(type == PlayHand::Hand_Bomb_Jokers)
    {
        showAnimaltion(JokerBomb);
    }
    if(cards.isEmpty())
    {
        it->info->setPixmap(QPixmap(":/images/images/pass.png"));
        it->info->show();
    }
    updataPlayerCards(player);
}
//有问题
void GamePanel::hidePlayerDropCards(Player *player) {

    //QMap<Player*,PlayerContext>::iterator it = m_contextMap.find(player);
    QMap<Player*,PlayerContext>::iterator it = m_contextMap.find(player);
    if(it != m_contextMap.end())
    {
        if(it->lastCards.isEmpty())
        {
            it->info->hide();
        }
        else
        {
            // Cards --> Card
            QVector<Card> list = it->lastCards.toCardList();
            for(auto last = list.begin(); last != list.end(); ++last)
            {
                m_cardmap[*last]->hide();
            }
        }
        it->lastCards.clear();
    }

}
//void GamePanel::hidePlayerDropCards(Player *player)
//{
//    auto it = m_contextMap.find(player);
//    if(it !=m_contextMap.end())
//    {

//        if((*it).lastCards.isEmpty())
//        {
//            (*it).info->hide();
//        }
//        else
//        {
//            QVector<Card> list = (*it).lastCards.toCardList();
//            for(auto last = list.begin();last!=list.end();last++)
//            {
//                m_cardmap[*last]->hide();
//            }
//        }
//        (*it).info->clear();
//    }
//}

QPixmap GamePanel::loadRoleImage(Player::Sex sex, Player::Direction direct, Player::Role role)
{
    QVector<QString> lordman;
    QVector<QString> lordwoman;
    QVector<QString> farmerman;
    QVector<QString> farmerwoman;
    QImage images;
    int random = QRandomGenerator::global()->bounded(2);
    lordman<<":/images/images/lord_man_1.png"<<":/images/images/lord_man_2.png";
    lordwoman<<":/images/images/lord_woman_1.png"<<":/images/images/lord_woman_2.png";
    farmerman<<":/images/images/farmer_man_1.png"<<":/images/images/farmer_man_2.png";
    farmerwoman<<":/images/images/farmer_woman_1.png"<<":/images/images/farmer_woman_2.png";
    if(Player::Man == sex && Player::Lord == role)
    {
        images.load(lordman.at(random));
    }
    else if(Player::Woman == sex && Player::Lord == role)
    {
        images.load(lordwoman.at(random));
    }
    else if(Player::Man == sex && Player::Farmer == role)
    {
        images.load(farmerman.at(random));
    }
    else if(Player::Woman == sex && Player::Farmer == role)
    {
        images.load(farmerwoman.at(random));
    }
    QPixmap pixmap;
    if(direct == Player::Left)
    {
        pixmap = QPixmap::fromImage(images);
    }
    else
    {
        pixmap = QPixmap::fromImage(images.mirrored(true,false));
    }
    return pixmap;
}

void GamePanel::onCardSelected(Qt::MouseButton button)
{
    if(m_status == GameControl::dispatchCard ||
        m_status == GameControl::CallingLord)
    {
        return;
    }
    CardPanel *panel = static_cast<CardPanel*>(sender());
    if(panel->getOwner() != m_gamecontrol->getUserPlayer())
    {
        return;
    }
    m_curCardPanel = panel;
    if(button == Qt::LeftButton)
    {
        panel->setSelected(!panel->getSelected());
        updataPlayerCards(panel->getOwner());
        // 保存或删除扑克牌窗口对象
        QSet<CardPanel*>::const_iterator it = m_selectCards.find(panel);
        if(it == m_selectCards.constEnd())
        {
            m_selectCards.insert(panel);
        }
        else
        {
            m_selectCards.erase(it);
        }
    }
    else if(button == Qt::RightButton)
    {
        //出牌函数
        onUserPlayHand();
    }
}

void GamePanel::onUserPlayHand()
{
    //qDebug()<<"1";
    if(m_status != GameControl::PlaingHand)
    {
        return;
    }
    //qDebug()<<"2";
    if(m_gamecontrol->getCurPlayer() != m_gamecontrol->getUserPlayer())
    {
        return;
    }
    //qDebug()<<"3";
    //qDebug()<<"3";
    qDebug()<<"当前选中牌个数："<<m_selectCards.size();
    if(m_selectCards.isEmpty())
    {
        return;
    }
   // qDebug()<<"4";
    Cards cs;
    for(auto it = m_selectCards.begin();it!=m_selectCards.end();++it)
    {
        Card card = (*it)->getCard();
        cs.add(card);
    }
    //qDebug()<<"5";
    PlayHand hand(cs);
    PlayHand::HandType type = hand.getHandType();
    if(type == PlayHand::Hand_Unknown)
    {
        return;
    }
   // qDebug()<<"6";

    if(m_gamecontrol->getPendPlayer() !=m_gamecontrol->getUserPlayer())
    {
        Cards cards = m_gamecontrol->getPendCards();
        if(!hand.canBeat(PlayHand(cards)))
        {
            return;
        }
    }

    //qDebug()<<"7";
    m_gamecontrol->getCurPlayer()->playHand(cs);
    //m_countDown->stopCountDown();
    m_selectCards.clear();
}

void GamePanel::onUserPass()
{
    //m_countDown->stopCountDown();
    Player* userplayer = m_gamecontrol->getUserPlayer();
    Player* curplayer = m_gamecontrol->getCurPlayer();
    if(userplayer !=curplayer)
    {
        return;
    }
    Player* pendPlayer = m_gamecontrol->getPendPlayer();
    if(pendPlayer == userplayer || pendPlayer == nullptr)
    {
        return;
    }
    Cards empty;
    userplayer->playHand(empty);

    for(auto it = m_selectCards.begin();it!=m_selectCards.end();++it)
    {
        (*it)->setSelected(false);
    }
    m_selectCards.clear();
    updataPlayerCards(userplayer);

}



void GamePanel::initCountDown()
{
    m_countDown = new CountDown(this);
    m_countDown->move((width()-m_countDown->width())/2 , (height()-m_countDown->height())/2+30);
    connect(m_countDown,&CountDown::notMuchTime,this,[=](){
        //播放时间不足音乐
    });
    connect(m_countDown,&CountDown::timeout,this,&GamePanel::onUserPass);
    UserPlayer *userplaer = m_gamecontrol->getUserPlayer();
    connect(userplaer,&UserPlayer::startCountDown,this,[=](){
        if(m_gamecontrol->getPendPlayer()!=userplaer && m_gamecontrol->getPendPlayer() != nullptr)
        {
            m_countDown->showCountDown();
        }
    });
}
void GamePanel::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton)
    {
        QPoint pos = event->pos();
        if(!m_cardRect.contains(pos))
        {
            m_curCardPanel = nullptr;
        }
        else
        {
            QList<CardPanel*> list = m_usercard.keys();
            for(int i = 0 ;i<list.size();++i)
            {
                CardPanel *panel = list.at(i);
                if(m_usercard[panel].contains(pos) && m_curCardPanel != panel)
                {
                    panel->clicked();
                    m_curCardPanel = panel;
                }
            }
        }
    }
}
void GamePanel::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter p(this);
    p.drawPixmap(rect(),m_pixmap);
}
